package com.pg.agent.engine.client.model.action;

import com.pg.agent.engine.client.message.InPutMessage;
import com.pg.agent.engine.client.model.ComResponse;
import com.pg.agent.engine.client.model.llm.CallLog;
import com.pg.agent.engine.client.model.sop.Memory;
import com.pg.agent.engine.client.vo.AIAgentSession;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.StringJoiner;

/**
 * action
 *
 * @author huhuaping
 * @date 2023/11/3 11:13
 */
@Slf4j
@Data
public class Action implements Serializable {

    private static final long serialVersionUID = 9120606095133429205L;

    /**
     * action 名称
     */
    private String name;

    /**
     * action 角色
     */
    private String role;

    /**
     * 是否是用户
     */
    private boolean user = false;
    /**
     * 响应值
     */
    private List<String> response;

    /**
     * 响应字典 其他组件工具返回
     */
    private ComResponse resDict;

    /**
     * LLM结果调用
     */
    private CallLog callLog;

    private String agentResult;


    /**
     * 处理操作
     *
     * @return 返回记忆对象
     */
    public Memory process(AIAgentSession session) {

        // 包装响应值
        String responseAll = packResponse();
        //将里面对话的第三人称删了
        responseAll = responseAll.replace(name + ":", "");

        // 非用户打印。
        if (!user) {
            //System.out.println(name+"("+role+"):"+responseAll);
        }
        log.info("\n" + name + ":" + responseAll);
        // 存储到硬盘
        return new Memory(session.getExeId(), session.getDataId(), role, name, responseAll, responseAll);
    }

    public Memory process(InPutMessage message) {

        // 包装响应值
        String responseAll = packResponse();
        //将里面对话的第三人称删了
        responseAll = responseAll.replace(name + ":", "");

        // 非用户打印。
        if (!user) {
            //System.out.println(name+"("+role+"):"+responseAll);
        }
        log.info("\n" + name + "(" + role + "):" + responseAll);
        // 存储到硬盘
        return new Memory(message.getExeId(), message.getDataId(), role, name, responseAll, responseAll);
    }
    /**
     * 包装Response;
     *
     * @return String 包装好的Response
     */
    public String packResponse() {
        StringJoiner responseAll = new StringJoiner(" ");
        if (response != null) {
            for (String s : response) {
                responseAll.add(s);
            }
        }
        return responseAll.toString();
    }
}
